#!/usr/local/bin/php
<?php

/*
    Copyright (C) 2016 Deciso B.V.
    Copyright (C) 2004-2006 Scott Ullrich <sullrich@gmail.com>
    Copyright (C) 2005 Bill Marquette <bill.marquette@gmail.com>
    Copyright (C) 2006 Peter Allgeyer <allgeyer@web.de>
    Copyright (C) 2008 Ermal Luçi
    Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

require_once("config.inc");
require_once("filter.inc");
require_once("util.inc");
require_once("interfaces.inc");
require_once("XMLRPC_Client.inc") ;

/**
 * fetch carp vips from config with modified advskew for the backup host to use
 * @return array
 */
function get_vip_config_section(): array
{
    global $config;

    if (!isset($config['virtualip']['vip'])) {
        return [];
    }
    $temp = array();
    $temp['vip'] = array();
    foreach($config['virtualip']['vip'] as $section) {
        if (!empty($section['vhid'])) {
            if (isset($section['advskew']) && $section['advskew'] !== '') {
                $section['advskew'] = min(intval($section['advskew']) + 100, 254);
            }
            $temp['vip'][] = $section;
        }
    }
    return $temp;
}

/**
 *  validate remote config version
 * @param string $url backup url
 * @param string $username remote username
 * @param string $password remote password
 * @param string $method xmlrpc method to call
 * @return boolean
 */
function carp_check_version($url, $username, $password, $method = 'opnsense.firmware_version')
{
    $client = new SimpleXMLRPC_Client($url,240);
    $client->setCredentials($username, $password);
    if ($client->query($method)) {
        $remote_version = $client->getResponse();
    } else {
        // propagate error to log
        $error = "An error occurred while attempting XMLRPC sync with username {$username} and {$url} " .  $client->error ;
        log_error($error);
        file_notice($error);
        // print communication details on failure
        echo $client->getDetails();
        return false ;
    }

    if (!is_array($remote_version) && trim($remote_version) == "Authentication failed") {
        $error = "An authentication failure occurred while trying to access {$url} ({$method}).";
        log_error($error);
        file_notice($error);
        return false;
    }

    return true;
}

/**
 * traverse config structure and remove (unset) all "nosync" items
 * @param array $cnf_structure pointer to config data
 */
function remove_nosync(&$cnf_structure)
{
    if (!is_array($cnf_structure)) {
        return false;
    } else {
        foreach ($cnf_structure as $cnf_key => &$cnf_data) {
            if (is_array($cnf_data) && isset($cnf_data['nosync'])) {
                unset($cnf_structure[$cnf_key]);
            } else {
                remove_nosync($cnf_data);
            }
        }
    }
}

/**
 * find config section by reference (dot notation)
 * for example system.user.0 points to the first enty in <system><user> xml config section
 * @param array $cnf_structure_in pointer to config data
 * @param array $cnf_structure_out pointer to config data
 * @param string $reference reference pointer (system.user for example)
 * @return null
 */
function copy_conf_section(&$cnf_structure_in, &$cnf_structure_out,  $reference)
{
    $cnf_path = explode('.', $reference);
    $cnf_path_depth = 1;
    foreach ($cnf_path as $cnf_section) {
        if (isset($cnf_structure_in[$cnf_section])) {
            // reference found, create output structure when the data to copy lies deeper.
            // for example wireless.clone.0 would only copy the first wireless clone, returns false on not found
            $cnf_structure_in = &$cnf_structure_in[$cnf_section];
            if ($cnf_path_depth < count($cnf_path)) {
                if (!isset($cnf_structure_out[$cnf_section])) {
                    $cnf_structure_out[$cnf_section] = array();
                }
                $cnf_structure_out = &$cnf_structure_out[$cnf_section];
            } else {
              $cnf_structure_out[$cnf_section] = $cnf_structure_in;
            }
        } else {
            // if source entry doesn't exist, make sure we copy an empty entry to the remote side
            // otherwise there's no way to know data has been removed
            if (!isset($cnf_structure_out[$cnf_section])) {
                $cnf_structure_out[$cnf_section] = array();
            }
            $cnf_structure_out = &$cnf_structure_out[$cnf_section];
        }
        $cnf_path_depth++;
    }
}

/**
 * traverse config structure and remove (unset) all "nosync" items
 * @param string $url backup url
 * @param string $username remote username
 * @param string $password remote password
 * @param array $sections sections to transfer
 * @param string $method xmlrpc method to call
 * @return boolean
 */
function carp_sync_xml($url, $username, $password, $sections, $method = 'opnsense.restore_config_section')
{
    global $config;

    $transport_data = array();
    foreach ($sections as $section) {
        switch ($section) {
            case 'virtualip':
                // Sync only eligible VIPs to the backup host
                $transport_data[$section] = get_vip_config_section();
                break;
            default:
                copy_conf_section($config, $transport_data, $section);
        }
    }

    // remove items which may not be synced
    remove_nosync($transport_data);

    // ***** post proccessing *****
    // dhcpd, unchanged from legacy code (may need some inspection later)
    if (is_array($transport_data['dhcpd'])) {
        foreach($transport_data['dhcpd'] as $dhcpif => $dhcpifconf) {
            if (isset($dhcpifconf['failover_peerip']) && $dhcpifconf['failover_peerip'] != '') {
                $int = guess_interface_from_ip($dhcpifconf['failover_peerip']);
                $intip = find_interface_ip($int);
                $transport_data['dhcpd'][$dhcpif]['failover_peerip'] = $intip;
            }
        }
    }

    // when syncing users, send last used uid/gid over
    if (in_array('system.user', $sections)) {
        if (!isset($transport_data['system'])) {
            $transport_data['system'] = array();
        }
        $transport_data['system']['nextuid'] = $config['system']['nextuid'];
        $transport_data['system']['nextgid'] = $config['system']['nextgid'];
    }

    $client = new SimpleXMLRPC_Client($url,240);
    $client->setCredentials($username, $password);
    if ($client->query($method, $transport_data)) {
        $response = $client->getResponse();
    } else {
        // propagate error to log
        $error = "An error occurred while attempting XMLRPC sync with username {$username} and {$url} " .  $client->error ;
        log_error($error);
        file_notice($error);
        // print communication details on failure
        echo $client->getDetails();
        return false ;
    }

    if (!is_array($response) && trim($response) == "Authentication failed") {
        $error = "An authentication failure occurred while trying to access {$url} ({$method}).";
        log_error($error);
        file_notice($error);
        exit;
    }

    return true;
}

if (file_exists('/var/run/booting')) {
    return;
}

if (isset($config['hasync']) && is_array($config['hasync'])) {
    $hasync = $config['hasync'];

    if (empty($hasync['synchronizetoip'])) {
        log_error("Config sync not being done because of missing sync IP (this is normal on secondary systems).");
        exit;
    }
    if (is_ipaddrv6($hasync['synchronizetoip'])) {
        $hasync['synchronizetoip'] = "[{$hasync['synchronizetoip']}]";
    }

    // determine target url
    if (substr($hasync['synchronizetoip'],0, 4) == 'http') {
        // URL provided
        if (substr($hasync['synchronizetoip'], strlen($hasync['synchronizetoip'])-1, 1) == '/') {
            $synchronizeto = $hasync['synchronizetoip']."xmlrpc.php";
        } else {
            $synchronizeto = $hasync['synchronizetoip']."/xmlrpc.php";
        }
    } elseif (!empty($config['system']['webgui']['protocol'])) {
        // no url provided, assume the backup is using the same settings as our box.
        $port = $config['system']['webgui']['port'];
        if (!empty($port)) {
            $synchronizeto =  $config['system']['webgui']['protocol'] . '://'.$hasync['synchronizetoip'].':'.$port."/xmlrpc.php";
        } else {
            $synchronizeto =  $config['system']['webgui']['protocol'] . '://'.$hasync['synchronizetoip']."/xmlrpc.php" ;
        }
    }

    // map configuration directives to config sections
    $section_cnf = array();
    $section_cnf['synchronizerules'] = 'filter';
    $section_cnf['synchronizenat'] = 'nat';
    $section_cnf['synchronizealiases'] = 'OPNsense.Firewall.Alias';
    $section_cnf['synchronizedhcpd'] = 'dhcpd';
    $section_cnf['synchronizestaticroutes'] = 'staticroutes,gateways';
    $section_cnf['synchronizevirtualip'] = 'virtualip';
    $section_cnf['synchronizecerts'] = 'cert,ca,crl';
    $section_cnf['synchronizeusers'] = 'system.user,system.group';
    $section_cnf['synchronizeauthservers'] = 'system.authserver';
    $section_cnf['synchronizeschedules'] = 'schedules';
    $section_cnf['synchronizewidgets'] = 'widgets';

    foreach (plugins_xmlrpc_sync() as $syncid => $syncconf) {
        $section_cnf['synchronize'.$syncid] = $syncconf['section'];
    }

    $sections = array();
    foreach ($section_cnf as $cnf_key => $cnf_sections) {
        if (isset($hasync[$cnf_key])) {
            foreach (explode(',', $cnf_sections) as $section) {
                $sections[] = $section;
            }
        }
    }

    if (count($sections) <= 0) {
        log_error("Nothing has been configured to be synched. Skipping....");
        exit;
    }

    $username = empty($hasync['username']) ? "root" : $hasync['username'];
    if (!carp_check_version($synchronizeto, $username, $hasync['password'])) {
        exit;
    }

    carp_sync_xml($synchronizeto, $username, $hasync['password'], $sections);

    if (count($argv) <= 1 || $argv[1] != 'restart' ) {
        // only sync data, no reload
        // TODO: config sync probably needs more thinking, but when we always force a reload
        // TODO: the machine tends to get sloppy
        exit;
    }
    $client = new SimpleXMLRPC_Client($synchronizeto, 240);
    $client->setCredentials($username, $hasync['password']);
    if ($client->query("opnsense.filter_configure")) {
        $response = $client->getResponse();
    } else {
        // propagate error to log
        $error = "An error occurred while attempting XMLRPC sync with username {$username} and {$url} " .  $client->error ;
        log_error($error);
        file_notice($error);
        // print communication details on failure
        echo $client->getDetails();
        return false ;
    }

    if (!is_array($response) && trim($response) == "Authentication failed") {
        $error = "An authentication failure occurred while trying to access {$url} ({$method}).";
        log_error($error);
        file_notice($error);
        exit;
    }

    log_error("Filter sync successfully completed with {$synchronizeto}.");
}
